const cvs = {};
//画箭头
function drawArrow(dash, ctx, xA, yA, xB, yB, color, l = 5, θ = 30) {
  let L = 5;
  let a = Math.atan2((yB - yA), (xB - xA));
  let xC = xB - L * Math.cos(a + θ * Math.PI / 180); // θ=30
  let yC = yB - L * Math.sin(a + θ * Math.PI / 180);
  let xD = xB - L * Math.cos(a - θ * Math.PI / 180);
  let yD = yB - L * Math.sin(a - θ * Math.PI / 180);
  ctx.beginPath();
  if (dash == 'dash') {
    ctx.setLineDash([2]);
  }
  ctx.moveTo(xA, yA);
  ctx.lineTo(xB, yB);
  ctx.moveTo(xC, yC);
  ctx.lineTo(xB, yB);
  ctx.lineTo(xD, yD);
  // ctx.lineWidth = 5;//箭头的粗细，可以根据需要自己选择
  ctx.strokeStyle = color;
  ctx.stroke();
}
//画交换机
function addimgS(ctx, texta, textb, img, x, y,) {
  ctx.drawImage(img, x, y, 100, 40);
  ctx.font = '12px 黑体'
  ctx.fillStyle = "#d3c467";
  ctx.fillText(textb, x, y - 10);
  ctx.font = '12px 黑体'
  ctx.fillStyle = "#d3c467";
  ctx.fillText(texta, x, y - 30);

}
function addimgM(ctx, texta, textb, img, x, y,) {
  ctx.drawImage(img, x, y, 90, 40);
  ctx.font = '12px 黑体'
  ctx.fillStyle = "#d3c467";
  ctx.fillText(texta, x + 100, y + 10);
  ctx.font = '12px 黑体'
  ctx.fillStyle = "#d3c467";
  ctx.fillText(textb, x + 100, y + 30);
}
function addimgE(ctx, texta, textb, img, x, y,) {
  ctx.drawImage(img, x, y, 90, 40);
  ctx.font = '12px 黑体'
  ctx.fillStyle = "#d3c467";
  ctx.fillText(texta, x, y + 60);
  ctx.font = '12px 黑体'
  ctx.fillStyle = "#d3c467";
  ctx.fillText(textb, x, y + 80);
}
//画虚线矩形
function drawRect(ctx, x, y) {
  ctx.strokeStyle = '#fffddd';
  // 矩形的线条宽度
  ctx.lineWidth = 1;
  ctx.setLineDash([4, 7]);
  ctx.strokeRect(x, y, 200, 120);
}
cvs.drawArrow = function (dash, ctx, xA, yA, xB, yB, color) {
  drawArrow(dash, ctx, xA, yA, xB, yB, color);
};
cvs.addimgS = function (ctx, texta, textb, img, x, y,) {
  addimgS(ctx, texta, textb, img, x, y,);
};
cvs.addimgM = function (ctx, texta, textb, img, x, y,) {
  addimgM(ctx, texta, textb, img, x, y,);
};
cvs.addimgE = function (ctx, texta, textb, img, x, y,) {
  addimgE(ctx, texta, textb, img, x, y,);
};
cvs.drawRect = function (ctx, x, y) {
  drawRect(ctx, x, y);
};
export default cvs;
